/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package k3tool.tinyex.model;

import nu.xom.Attribute;
import nu.xom.Element;

/**
 *
 * @author Jeff Huang
 */
public abstract class TableOfContentItemHelper {
    /**
     * Generate an XHTML element recursively for an TOC item.
     * 
     * @param item
     * @return
     */
    public static Element generateXHtmlElement(TableOfContentItem item, String prefix, int itemNum){
        Element tocElement = new Element("li");
        Element refElement = new Element("a");
        refElement.addAttribute(new Attribute("href", item.getHref()));
        String itemOrder = String.format("%s.%d", prefix, itemNum);
        refElement.appendChild(String.format("%s %s", itemOrder, item.getValue()));
        tocElement.appendChild(refElement);

        if(item.getSubItemList()!=null && !item.getSubItemList().isEmpty()){
            int itemCount = 0;
            Element ulEl = new Element("ul");
            for(TableOfContentItem subItem : item.getSubItemList()){
                ++itemCount;
                Element subEl = generateXHtmlElement(subItem, itemOrder, itemCount);
                ulEl.appendChild(subEl);
            }
            tocElement.appendChild(ulEl);
        }

        return tocElement;
    }

}
